package com.demo.util;

import com.demo.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class ResultUtil<T> {
    //1.status状态值：代表本次请求response的状态结果
    private Integer status;
    //2.response描述：对本次状态码的描述
    private String desc;
    //3.data数据：本次返回的数据
    private T data;

    /**
     * 成功，创建ResResultUtil：没data数据
     */
    public static ResultUtil success() {
        ResultUtil ResultUtil = new ResultUtil();
        ResultUtil.setResultUtilCode(ResultCode.SUCCESS);
        return ResultUtil;
    }

    /**
     * 成功，创建ResResultUtil：有data数据
     */
    public static ResultUtil success(Object data) {
        ResultUtil ResultUtil = new ResultUtil();
        ResultUtil.setResultUtilCode(ResultCode.SUCCESS);
        ResultUtil.setData(data);
        return ResultUtil;
    }

    /**
     * 失败，指定status、desc
     */
    public static ResultUtil failure(Integer status, String desc) {
        ResultUtil ResultUtil = new ResultUtil();
        ResultUtil.setStatus(status);
        ResultUtil.setDesc(desc);
        return ResultUtil;
    }

    /**
     * 失败，指定ResultUtilCode枚举
     */
    public static ResultUtil failure(ResultCode ResultCode) {
        ResultUtil ResultUtil = new ResultUtil();
        ResultUtil.setResultUtilCode(ResultCode);
        return ResultUtil;
    }

    /**
     * 把ResultUtilCode枚举转换为ResResultUtil
     */
    private void setResultUtilCode(ResultCode code) {
        this.status = code.code();
        this.desc = code.message();
    }

}
